<template>
  <div v-if="showSalesman" :label="title" prop="salesmanId" :rules="rules">
    <form-remote-select
      v-model="selected"
      :url="salesmanUrl"
      :disabled="disabled"
      :placeholder="placeholder"
      @change="change"
    />
  </div>
</template>
<script>
import adminSupport from 'lola-admin-support-el'

export default {
  name: 'DutyUsers',
  props: {
    mode: {
      type: String,
      default: 'form'
    },
    value: {// 设置value为props属性-必须
      type: [String],
      default: ''
    },
    dutyId: {
      type: [String],
      default: null
    },
    lable: {
      type: [String],
      default: '员工'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: '请选择'
    }
  },
  data() {
    return {
      salesmanUrl: '',
      showSalesman: false,
      rules: { required: true, message: '必填', trigger: 'blur' },
      title: null
    }
  },
  computed: {
    selected: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    }
  },
  mounted() {
    // 是否启动验证
    if (this.mode === 'form') {
      this.rules = adminSupport.lib.ruleStore.def
      this.title = this.lable
    } else {
      this.title = null
    }
    this.salesmanUrl = adminSupport.lib.service.conf.api.get('portal').url + '/api/user/findAll/duty/' + this.dutyId
    if (this.dutyId === null) {
      this.showSalesman = true
    } else {
      this.showSalesman = this.$store.state.user.token.dutyId !== this.dutyId
    }
    if (!this.showSalesman) {
      setTimeout(() => {
        this.$emit('input', this.$store.state.user.token.id)
        this.$emit('change', this.$store.state.user.token.id)
      }, 1000)
    }
  },
  methods: {
    change(current) {
      this.$emit('change', current)
    }
  }
}
</script>
